﻿'    DotNetIRCD - An IRC Daemon written in VB.Net
'    Copyright (C) 2012 GeorgeBush
'
'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program. If not, see <http://www.gnu.org/licenses/>.

Imports System.IO
Imports System.Xml.Serialization

''' <summary>
''' This Class represents the Servers Settings which will be stored in an XML file.
''' </summary>
<Serializable()> _
Public NotInheritable Class ServerConfiguration

    ''' <summary>
    ''' Saves the Server Settings to file
    ''' </summary>
    ''' <param name="FileName">The FileName to save the Server Settings to.</param>
    Public Sub Save(ByVal FileName As String)

        ' Open the file for writing. If it exists, create a new one.
        Dim objStreamWriter As New StreamWriter(FileName, False)

        ' Create the XML Serializer
        Dim Serializer As New XmlSerializer(GetType(ServerConfiguration))

        ' Serialize this class
        Serializer.Serialize(objStreamWriter, Me)

        ' Close, Dispose and NULL the StreamWriter
        objStreamWriter.Close()
        objStreamWriter.Dispose()
        objStreamWriter = Nothing

    End Sub

    ''' <summary>
    ''' Loads a copy of the Server Settings from file
    ''' </summary>
    ''' <param name="FileName">The FileName to load the Server Settings from.</param>
    ''' <returns>ServerConfigurationClass containing the Server Settings</returns>
    Public Shared Function Load(ByVal FileName As String) As ServerConfiguration

        ' Check that the file exists
        If Not My.Computer.FileSystem.FileExists(FileName) Then
            Logging.Log(LogTypeEnum.Error, LogTargetEnum.Console, "Failed to load settings from " & FileName)
            Return Nothing
        End If

        ' Create a new object to load the settings into
        Dim tSettings As New ServerConfiguration

        ' Create new StreamReader
        Dim objStreamReader As New StreamReader(FileName)

        ' Create the Serializer
        Dim Serializer As New XmlSerializer(GetType(ServerConfiguration))

        ' Attempt to DeSerialize the Settings File
        Try
            tSettings = CType(Serializer.Deserialize(objStreamReader), ServerConfiguration)
        Catch ex As Exception
            Logging.Log(LogTypeEnum.Error, LogTargetEnum.Console, "Failed to load settings from " & FileName)
            Logging.Log(LogTypeEnum.Error, LogTargetEnum.Console, ex.Message)
        End Try

        ' Return the new DeSerialized class
        Return tSettings

    End Function

    ''' <summary>
    ''' The name of this server
    ''' </summary>
    Public Property ServerName As String

    ''' <summary>
    ''' The network this server is part of
    ''' </summary>
    Public Property NetworkName As String

    ''' <summary>
    ''' The logging provider for this server
    ''' </summary>
    <XmlElement(ElementName:="Logging")> _
    Public Property Logger As New LoggingProvider

    ''' <summary>
    ''' Stores the binding defined for this server
    ''' </summary>
    Public Property Bindings As New List(Of BindPoint)



End Class
